package es.udc.fic.hamNeggs.pojo;

import es.udc.fic.hamNeggs.pojo.UserStories.State;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Entity
@Table(name = "hamneggs.projects")
public class Project {
    @Id
    @GeneratedValue
    @Column(name = "prj_id")
    private Long             id;

    @Column(name = "prj_name")
    private String           name;

    @Column(name = "prj_dateStart")
    private Date             dateStart;

    @Column(name = "prj_dateEnd")
    private Date             dateEnd;

    @OneToMany(mappedBy = "project")
    private Set<UserStories> userStories;

    @OneToMany(mappedBy = "project")
    private Set<Sprint>      sprints;

    @OneToMany(mappedBy = "project")
    private Set<Works>       worker;

    @OneToMany(mappedBy = "project")
    private Set<Meeting>     meetings;

    public Project() {

    }

    public List<Meeting> getMeetings() {
        List<Meeting> lmeetings = new ArrayList<Meeting>();
        lmeetings.addAll(meetings);
        Collections.sort(lmeetings,
                new Comparator<Meeting>() {
                    public int compare(Meeting o1, Meeting o2) {
                        return o1.getId().intValue() - o2.getId().intValue();
                    }
                });
        return lmeetings;
    }

    public void setMeetings(Set<Meeting> meetings) {
        this.meetings = meetings;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Project(final String name, final Date dateStart, final Date dateEnd) {
        this.name = name;
        this.dateStart = dateStart;
        this.dateEnd = dateEnd;
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public Date getDateStart() {
        return dateStart;
    }

    public void setDateStart(final Date dateStart) {
        this.dateStart = dateStart;
    }

    public Date getDateEnd() {
        return dateEnd;
    }

    public void setDateEnd(final Date dateEnd) {
        this.dateEnd = dateEnd;
    }

    public Long getId() {
        return id;
    }

    public List<Works> getUsers() {
        return getWorker();
    }

    public List<UserStories> getUserStories() {
        List<UserStories> luserStories = new ArrayList<UserStories>();
        luserStories.addAll(userStories);
        Collections.sort(luserStories,
                new Comparator<UserStories>() {
                    public int compare(UserStories o1, UserStories o2) {
                        return o1.getId().intValue() - o2.getId().intValue();
                    }
                });
        return luserStories;
    }

    public List<Sprint> getSprints() {
        List<Sprint> lsprints = new ArrayList<Sprint>();
        lsprints.addAll(sprints);
        Collections.sort(lsprints,
                new Comparator<Sprint>() {
                    public int compare(Sprint o1, Sprint o2) {
                        return o1.getId().intValue() - o2.getId().intValue();
                    }
                });
        return lsprints;
    }

    public List<Works> getWorker() {
        List<Works> lworker = new ArrayList<Works>();
        lworker.addAll(worker);
        Collections.sort(lworker,
                new Comparator<Works>() {
                    public int compare(Works o1, Works o2) {
                        return o1.getUser().getId().intValue() - o2.getUser().getId().intValue();
                    }
                });
        return lworker;
    }

    public void setUserStories(Set<UserStories> userStories) {
        this.userStories = userStories;
    }

    public void setSprints(Set<Sprint> sprints) {
        this.sprints = sprints;
    }

    public void setWorker(Set<Works> worker) {
        this.worker = worker;
    }

    public int getUserStoriesDone() {
        int cuenta = 0;
        for (UserStories story : userStories) {
            if (story.getState().getString().equalsIgnoreCase(State.Validated.getString()))
                cuenta++;
        }
        return cuenta;
    }

    public int getSPEstimated() {
        int cuenta = 0;
        for (UserStories story : userStories) {
            cuenta += story.getEstimatedstoryPoints();
        }
        return cuenta;
    }

    // @OneToMany(mappedBy = "project")
    // private Set<UserStories> userStories;

    public void setOneUserStory(UserStories ust) {
        if (userStories == null)
            userStories = new HashSet<UserStories>();
        userStories.add(ust);
    }

    //
    // @OneToMany(mappedBy = "project")
    // private Set<Sprint> sprints;
    public void setOneSprint(Sprint spr) {
        if (sprints == null)
            sprints = new HashSet<Sprint>();
        sprints.add(spr);
    }

    //
    // @OneToMany(mappedBy = "project")
    // private Set<Works> worker;
    public void setOneWorker(Works wrk) {
        if (worker == null)
            worker = new HashSet<Works>();
        worker.add(wrk);
    }

    //
    // @OneToMany(mappedBy = "project")
    // private Set<Meeting> meetings;
    public void setOneMeeting(Meeting mtg) {
        if (meetings == null)
            meetings = new HashSet<Meeting>();
        meetings.add(mtg);

    }

}
